package com.sx.zongzhi.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.bean.BuildingListBean;
import com.sx.zongzhi.bean.MsgEvent;
import com.sx.zongzhi.presenter.BuildingAddPest;
import com.sx.zongzhi.utils.MyRxBus;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.CommentView;
import com.sx.zongzhi.widget.CustomDialog;
import com.sx.zongzhi.widget.LoadDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 楼宇详情
 */
public class BuildingDetailActivity extends BaseActivity implements CommentView {

    @BindView(R.id.tv_BuildinglName) TextView tv_BuildinglName;
    @BindView(R.id.tv_Type) TextView tv_Type;
    @BindView(R.id.tv_Structure) TextView tv_Structure;
    @BindView(R.id.tv_Location) TextView tv_Location;
    @BindView(R.id.tv_Owner) TextView tv_Owner;
    @BindView(R.id.tv_Person) TextView tv_Person;
    @BindView(R.id.tv_Phone) TextView tv_Phone;

    private BuildingListBean.RowsBean buildingDetail;
    private String orgId="";

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_building_detail);
        ButterKnife.bind(this);
        setTitleName(R.string.building_detail);
        buildingDetail= (BuildingListBean.RowsBean) getIntent().
                getSerializableExtra("BuildingListBean.RowsBean");
        orgId = getIntent().getStringExtra("orgId");
        initViews();
        initData();
    }

    private void initViews() {
        BuildingListBean.PermissionBean userPermission = (BuildingListBean.PermissionBean)
                getIntent().getSerializableExtra("userPermission");
        if (userPermission != null && userPermission.isDeleteBuilddatas()){
            setRightImage(R.mipmap.icon_nav_delete);
        }
        if (userPermission != null && userPermission.isUpdateBuilddatas()){
            setRightImage2(R.mipmap.icon_nav_edit);
        }
    }

    private void initData() {
        if (buildingDetail!=null){
            tv_BuildinglName.setText(buildingDetail.getBuildingname());
            tv_Type.setText(buildingDetail.getType()!=null?buildingDetail.getType().getDisplayName():"");
            tv_Structure.setText(buildingDetail.getBuildingstructures()!=null?
                buildingDetail.getBuildingstructures().getDisplayName():"");
            tv_Location.setText(buildingDetail.getBuildingaddress());
            tv_Owner.setText(buildingDetail.getOwner());
            tv_Person.setText(buildingDetail.getResponsibleperson());
            tv_Phone.setText(buildingDetail.getPhone());
        }else {
            ToastUtils.showToast(this,"系统异常 稍后再试");
        }
    }



    @OnClick({R.id.imv_Right,R.id.imv_Right2})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.imv_Right://删除
                createDelete();
                break;
            case R.id.imv_Right2://编辑
                if (buildingDetail!=null){
                    startActivity(new Intent(this,BuildingAddActivity.class)
                            .putExtra("BuildingListBean.RowsBean",buildingDetail)
                            .putExtra("orgId",orgId));
                    finish();
                }else {
                    ToastUtils.showToast(this,"未获取到详情 编辑失败");
                }
                break;
        }
    }

    private void createDelete() {
        CustomDialog.Builder builder=new CustomDialog.Builder(this);
        CustomDialog dialog=builder.setMessage("确定删除该楼宇信息?").
                setBackButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).setConfirmButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                LoadDialog.show(BuildingDetailActivity.this,"删除中");
                new BuildingAddPest(BuildingDetailActivity.this).deleteBuilding(buildingDetail.getId());
                dialog.dismiss();
            }
        }).create();
        dialog.show();
    }


    @Override
    public void commentSuccess(Boolean result) {
        if (result){
            MyRxBus.get().post(new MsgEvent<>("BuildingActivity",1,"删除成功"));
            ToastUtils.showToast(this,"删除成功");
            finish();
        }
    }

    @Override
    public void showError(String message) {
        LoadDialog.dismiss(this);
        ToastUtils.showToast(this,message);
    }
}
